package com.me.study.JVM;

import com.me.common.util.SleepUtil;

/**
 * JVM内存分配担保机制
 *
 * @author ME
 * @date   2020/5/8
 */
public class HandlePromotionFailure {
    private static final int _1MB = 1024 * 1024;

    public static void main(String[] args) {
        /**
         * 配置 VM options：
         * -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:+UseSerialGC
         */
        testHandlePromotion();
    }

    private static void testHandlePromotion() {
        byte[] allocation1,allocation2,allocation3,allocation4;

        System.out.println("before allocation1: sleep 10");
        // SleepUtil.seconds(10);
        allocation1 = createByteArr(2 * _1MB);

        System.out.println("before allocation2: sleep 10");
        // SleepUtil.seconds(10);
        allocation2 = createByteArr(2 * _1MB);

        System.out.println("before allocation3: sleep 10");
        // SleepUtil.seconds(10);
        allocation3 = createByteArr(2 * _1MB);

        System.out.println("before allocation4: sleep 10");
        // SleepUtil.seconds(10);
        allocation4 = createByteArr(4 * _1MB);

        SleepUtil.seconds(1000);
    }

    private static byte[] createByteArr(int size) {
        byte[] byteArr = new byte[size];
        for (byte bb : byteArr) {
            bb = -128;
        }
        return byteArr;
    }
}
